<template>
  <el-container class="app-container">
    <el-header class="m-t-20">
      <el-button type="primary" icon="el-icon-plus" @click="addOne">创建</el-button>
    </el-header>

    <!--表格-->
    <table-view
      :editBtnMap="true"
      :editBtnMapList="['update', 'delete']"
      :columns="tableContent"
      :pageList="columnData"
      @edit-btnclick="editBtnClick"
    >
    </table-view>

    <!--分页-->
    <pagination-view :pagesize="pageInfo.pageSize"
                     :currentPage="pageInfo.pageNumber"
                     :total="total"
                     @size-change="sizeChange"
                     @current-change="pageChange"
    ></pagination-view>

    <el-dialog title="新增标签" :visible.sync="dialogVisible" width="60%"
               :destroy-on-close="true">
      <el-form ref="form" :model="form" :rules="rules" label-width="120px" style="width: 90%">
        <el-form-item prop="labelName" label="标签名称:">
          <el-input v-model.trim="form.labelName"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">保存</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>

    <el-footer></el-footer>
  </el-container>
</template>

<script>
  import TableView from '@/components/TableView'
  import ImgUpload from '@/components/ImgUpload'

  import PaginationView from '@/components/PaginationView'
  // import pageApi from '@/api/operation/productSetting'

  export default {
    name: 'list',
    components: {
      TableView,
      PaginationView,
      ImgUpload
    },
    data() {
      return {
        spaceOptions:[],
        statusOptions:[{label:'全部',value:''},{label:'正常',value:'1'},{label:'关闭',value:'0'}],
        dialogVisible:false,
        tableContent:[{
          field: 'productName',
          label: '名称'
        }, {
          field: 'productName',
          label: '备注'
        }],
        columnData: [{}, {}],
        customWidth: '',
        pageInfo: {
          pageNum: 1,
          pageSize: 20,
          title: ''
        },
        total: 0,
        form: {
          costAmount: '',
          intro: '',
          memo: '',
          title: '',
          addressDetail: '',
          addressName: '',
          saleAmount: '',
          sort: ''
        },
        rules: {
          productName: [
            {required: true, trigger: 'blur', message: '名称不能为空'},
            {min: 1, max: 14, message: '长度不能超过14个汉字'}
          ],
          intro: [
            {required: true, trigger: 'blur', message: '简介不能为空'},
            {min: 1, max: 20, message: '长度不能超过20个汉字'}
          ],
          durationTime: [{required: true, trigger: 'change', message: '请选择套餐时长'}],
          sort: [{required: true, trigger: 'blur', message: '请填写排序'}],
          enable: [{required: true, trigger: 'change', message: '请选择套餐状态'}],
          tag: [{min: 1, max: 4, trigger: 'blur', message: '长度不能超过4个汉字'}],
        }
      }
    },
    watch: {},
    methods: {
      addOne() {
        this.dialogVisible = true
      },

      changeUserStatus: function (id, enable) {
        pageApi.changeEnable(id, enable).then((res) => {
          this.handleMessage(res)
          if (res.code === '200') this.getData()
        })
      },
      onSubmit() {
        let form = this.form
        let dialogType = this.dialogType
        form.duration = form.durationTime + (form.productTypeId == 1 ? '个月' : '天')
        form.enable = Number(form.enable)
        this.$refs['form'].validate((valid) => {
          if (valid) {
            switch (dialogType) {
              case 'add':
                pageApi.create(form).then(res => {
                  this.handleRes(res)
                })
                break
              case 'update':
                pageApi.update(form).then(res => {
                  this.handleRes(res)
                })
                break

            }
          }
        })
      },
      handleRes(res) {
        this.handleMessage(res)
        if (res.code === '200') {
          this.dialogVisible = false
          this.getData()
        }
      },
      // pageSize变化函数
      sizeChange(val) {
        this.pageInfo.pageSize = val
        this.getData()
      },
      // page变化函数
      pageChange(val) {
        this.pageInfo.pageNumber = val
        this.getData()
      },
      editBtnClick(val,index, type) {
        switch (type) {
          case 'update':
            pageApi.getById(val.courseProductId).then(res => {
              this.form = Object.assign({}, this.form, res.body)
            })
            this.dialogType = type
            this.dialogVisible = true
            break
          case 'delete':
            this.changeUserStatus(val.courseProductId, val.enable ? 0 : 1)
            break
        }
      },
      //拉取列表
      getData() {
        pageApi.getList(this.pageInfo).then(res => {
          this.columnData = res.body.records
          this.total = Number(res.body.total)
          this.pageInfo.pageNumber = Number(res.body.pageNumber) || 1
        })
      },
      handleClose(done) {
        let resetForm = {
          costAmount: '',
          courseProductId: '',
          duration: '',
          durationTime: '',
          enable: true,
          intro: '',
          memo: '',
          productName: '',
          saleAmount: '',
          sort: '',
          tag: ''
        }
        this.form = Object.assign({}, this.form, resetForm)
        done()
      }

    },
    mounted() {
      this.getData()
    }
  }
</script>

<style lang="scss" scoped>

</style>
